package chess;

import java.awt.Color;
import java.util.HashSet;
import java.util.Set;

public class King extends Piece{

	private String name;
	private String type;
	private Color color;
	private Coordinate co;
	private boolean moved= false;
	private boolean selected;

	public King(String name, String type, Color color, Coordinate co){

		this.name= name;
		this.type= type;
		this.color= color;
		this.co= co;
	}

	public Set<Coordinate> getPossibleCoordinates(){
		int coX= co.getX();
		int coY= co.getY();
		Set<Coordinate> posCoords= new HashSet<Coordinate>();
		Coordinate c, c1, c2;

		for(Direction d:Direction.values()) {
			c = new Coordinate(coX+d.getDx(), coY+d.getDy());
			if (GameRunner.board.isValid(c)){
				if (GameRunner.board.isOccupied(c)){
					if (GameRunner.board.getPiece(c).getColor()!= color){
						posCoords.add(c);
					} 
				} else posCoords.add(c);
			}
		}

		if (!moved) {
			c1 = new Coordinate(coX-4, coY);
			if (GameRunner.board.isOccupied(c1)) {
				Piece p1  = GameRunner.board.getPiece(c1);
				if (p1.getType() == "Rook") {
					if (!p1.hasMoved() && 
							!GameRunner.board.isOccupied(new Coordinate(coX-3, coY)) &&
							!GameRunner.board.isOccupied(new Coordinate(coX-2, coY)) &&
							!GameRunner.board.isOccupied(new Coordinate(coX-1, coY))) {
						posCoords.add(new Coordinate(coX-2, coY));
					}
				}
			}

			c2 = new Coordinate(coX+3, coY);
			if (GameRunner.board.isOccupied(c2)) {
				Piece p2 = GameRunner.board.getPiece(c2);
				if (p2.getType() == "Rook") {
					if (!p2.hasMoved() && 
							!GameRunner.board.isOccupied(new Coordinate(coX+2, coY)) &&
							!GameRunner.board.isOccupied(new Coordinate(coX+1, coY))) {
						posCoords.add(new Coordinate(coX+2, coY));
					}
				}
			}
		}

		return posCoords;
	}

	public void moveTo(Coordinate c){
		// castling
		if (co.getX()-c.getX()==2) {  // queen side
			Piece r1 = GameRunner.board.getPiece(co.getX()-4, co.getY());
			GameRunner.movePiece(r1, co.getX()-1, co.getY());
			GameRunner.whiteTurn = !GameRunner.whiteTurn;
		} else if (co.getX()-c.getX()==-2) { // king side
			Piece r2 = GameRunner.board.getPiece(co.getX()+3, co.getY());
			GameRunner.movePiece(r2, co.getX()+1, co.getY());
			GameRunner.whiteTurn = !GameRunner.whiteTurn;
		}
		
		// move king	
		this.setCoordinate(c);
		moved= true;
		
		
		
	}

	public Coordinate getCoordinate(){
		return co;
	}

	public String getType(){
		return type;
	}
	
	public void setCoordinate(Coordinate c, Grid g){
		co= c;
		g.setPieceToCoordinate(c, this);
	}

	public void setCoordinate(Coordinate c){
		co= c;
		GameRunner.board.setPieceToCoordinate(c,this);
	}

	public String getName(){
		return name;
	}

	public Color getColor(){
		return color;
	}

	public String toString(){
		return name + ", " + type + ", " + color + co.toString();
	}

	public boolean hasMoved() {
		// TODO Auto-generated method stub
		return moved;
	}

	@Override
	public void setSelected(boolean b) {
		selected = b;
	}

	@Override
	public boolean isSelected() {
		return selected;
	}

}